package com.hq.fxh.No1_Infromation.Infor.Fragment;
/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description : 资讯内容页
 * @Created ${data}
 * @Email xlwenhao1858@sina.com
 */

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.hq.fxh.Modle.AutoNewsListModle;
import com.hq.fxh.Modle.AutoNewsModle;
import com.hq.fxh.Modle.GetAutoNewsHomeModle;
import com.hq.fxh.Modle.KanTieListModle;
import com.hq.fxh.Modle.MyResponseBody;
import com.hq.fxh.Modle.MyString;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No1_Infromation.Infor.Activity.News_DetailsActivity;
import com.hq.fxh.No1_Infromation.Infor.Adapter.Help_Infor_GridView;
import com.hq.fxh.No1_Infromation.Infor.Adapter.Ladapter_infor;
import com.hq.fxh.Modle.SelectByCategoryIdRes;
import com.hq.fxh.R;
import com.hq.fxh.Utils.AppConstant;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;
import com.hq.fxh.Utils.Tool;
import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.CustomerView.PopupWindow.PopupWindow_PingBi;
import com.stx.xhb.xbanner.XBanner;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Subscriber;

import static android.R.attr.id;
import static android.media.CamcorderProfile.get;
import static com.baidu.location.h.j.P;
import static com.baidu.location.h.j.e;

public class Fragment_infor extends BaseFragment {
    protected View rootView;
    protected PullToRefreshListView listInfor;
    protected XBanner xbanner;
    protected GridView itemGridview;
    List<AutoNewsModle> newsDatas;
    View topBannerViewHolder;
    View gridviewViewHolder;
    Ladapter_infor adapter;
    String url2 = "http://img06.tooopen.com/images/20170824/tooopen_sy_222228119642.jpg";
    String url = "http://img06.tooopen.com/images/20170814/tooopen_sy_220490839847.jpg";
    List<SelectByCategoryIdRes.ValueBean> data;
    List<GetAutoNewsHomeModle.DataBean.BlockBean> block;
    List<GetAutoNewsHomeModle.DataBean.BannersBean> banners;
    int pager = 1;

    public void fetchData(int type, int id) {

        int cityCode = SharedPreferenceUtils.getCityCode(getContext());
        LogUtils.e("加载资讯数据参数：" + type + " " + id + "  " + cityCode);
        UserServiceImpl.GetNewsList(type, cityCode + "", id, new Callback<AutoNewsListModle>() {
            @Override
            public void onResponse(Call<AutoNewsListModle> call, Response<AutoNewsListModle> response) {
                LogUtils.e("加载资讯数据shib  :  " + response.body());
                AutoNewsListModle body = response.body();
                if (body == null) {
                } else {
                    List<AutoNewsModle> data = body.getData();
                    if (data == null) {

                    } else {
                        for (int i = 0; i < data.size(); i++) {
                            newsDatas.add(0, data.get(i));
                        }
                        adapter.notifyDataSetChanged();
                    }
                }
                listInfor.onRefreshComplete();
            }

            @Override
            public void onFailure(Call<AutoNewsListModle> call, Throwable t) {
                LogUtils.e("加载资讯数据shib  :  " + t.getMessage());
                listInfor.onRefreshComplete();
            }
        });

    }


    public void getAutoNewHome() {
        int memberId = SharedPreferenceUtils.getMemberId(getContext());
        LogUtils.e("id   " + memberId);
        UserServiceImpl.GetAutoRefreshHome(0, new Callback<GetAutoNewsHomeModle>() {
            @Override
            public void onResponse(Call<GetAutoNewsHomeModle> call, Response<GetAutoNewsHomeModle> response) {

                GetAutoNewsHomeModle body = response.body();
                if (body != null) {
                    GetAutoNewsHomeModle.DataBean data = body.getData();
                    banners = data.getBanners();
                    block = data.getBlock();
                    List<AutoNewsModle> news = data.getNews();
                    if (news == null) {

                    } else {
                        if (newsDatas == null) {
                            newsDatas = new ArrayList<>();
                        }

                        newsDatas.addAll(news);

                        adapter.setData(newsDatas);
                        adapter.notifyDataSetChanged();
                    }

                    setTopView();

                } else {
                    ResponseBody responseBody = response.errorBody();
                    try {
                        LogUtils.e(responseBody.string());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<GetAutoNewsHomeModle> call, Throwable t) {

            }
        });
    }

    private void setTopView() {
        if (block == null) {
        } else {
            Help_Infor_GridView help_infor_gridView = new Help_Infor_GridView(getContext(), itemGridview, block);
        }

        ArrayList<String> xbannerData = new ArrayList<>();
        if (banners == null || banners.size() == 0) {
            xbannerData.add(url);

        } else {
            for (int i = 0; i < banners.size(); i++) {
                xbannerData.add(banners.get(i).getPic());
            }
        }

        setXbannerData(xbannerData);
    }

    public static Fragment_infor newInstance() {
        Bundle args = new Bundle();
        Fragment_infor fragment = new Fragment_infor();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_infor_infor;
    }

    @Override
    protected void findView(View rootView) {
        topBannerViewHolder = View.inflate(getContext(), R.layout.xbanner, null);//轮播图
        gridviewViewHolder = View.inflate(getContext(), R.layout.gridview, null);//选项图
        listInfor = (PullToRefreshListView) rootView.findViewById(R.id.list_infor);
        xbanner = (XBanner) topBannerViewHolder.findViewById(R.id.xbanner);
        itemGridview = (GridView) gridviewViewHolder.findViewById(R.id.item_gridview);
        getAutoNewHome();
        //   fetchData();
        getData();
    }

    @Override
    protected void initData() {
        listInfor.setMode(PullToRefreshBase.Mode.BOTH);
        listInfor.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pager = 1;
                fetchData(1, newsDatas.get(0).getArticle_id());

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pager = 1 + pager;
                fetchData(2, newsDatas.get(newsDatas.size() - 1).getArticle_id());
            }
        });
    }


    public void getData() {


        ListView refreshableView = listInfor.getRefreshableView();
        refreshableView.addHeaderView(topBannerViewHolder);
        refreshableView.addHeaderView(gridviewViewHolder);
        adapter = new Ladapter_infor(new Ladapter_infor.InforCallBack() {

            @Override
            public void onClickPingBi(View view, int position, AutoNewsModle data) {
                View popview = View.inflate(getContext(), R.layout.popup_information_pingbi, null);
                new PopupWindow_PingBi().showPopupWindow(getActivity(), popview, view);


            }

            @Override
            public void onClickOpenDetail(int position, AutoNewsModle data) {

                Intent intent = new Intent(getContext(), News_DetailsActivity.class);
                intent.putExtra("url", data.getShare_url());
                intent.putExtra("type", AppConstant.Type_New);
                intent.putExtra("data", data);

                startActivity(intent);
            }
        });
        listInfor.setAdapter(adapter);
    }

    public void setXbannerData(final List<String> xbannerData) {
        LogUtils.e(" banner数据" + xbannerData.toString());


        xbanner.setData(xbannerData, null);

        xbanner.setmAdapter(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                String stringContent = xbannerData.get(position);

                Tool.setUrlToImage(getContext(), stringContent, (ImageView) view);
            }
        });


        xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
                LogUtils.e("xbanner:" + position);
            }
        });

        xbanner.stopAutoPlay();

    }


}
